package com.demo.schedule.medicine;

import com.alibaba.fastjson.JSON;
import com.demo.entity.Medicine;
import com.demo.entity.MessageRule;
import com.demo.entity.VtePreventiveDoctorAdvice;
import com.demo.enums.MessageRuleLoopWarningEnum;
import com.demo.mapper.MedicineMapper;
import com.demo.mapper.MessageRuleMapper;
import com.demo.mapper.VtePreventiveDoctorAdviceMapper;
import com.demo.schedule.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
@DependsOn("springContextUtil")
@Order(1)
@Component
public class MedicineRemindSchedule {

    public static ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);

    @Resource
    private MedicineMapper medicineMapper;
    @Resource
    private MessageRuleMapper messageRuleMapper;
    @Resource
    private VtePreventiveDoctorAdviceMapper vtePreventiveDoctorAdviceMapper;

    //@PostConstruct
    public void init() {
        List<Medicine> medicines = medicineMapper.queryListInEffective();

        for (Medicine medicine : medicines) {
            // 查询对应药物提醒的规则
            List<MessageRule> messageRules = messageRuleMapper.queryListInEffective(medicine.getMedicineId());
            if (messageRules == null || messageRules.size() == 0) {
                continue;
            }

            for (MessageRule messageRule : messageRules) {
                // 查询与当前药物关联的医嘱
                List<VtePreventiveDoctorAdvice> doctorAdvices = vtePreventiveDoctorAdviceMapper.queryPatientsByDrugId(medicine.getMedicineId());
                for (VtePreventiveDoctorAdvice doctorAdvice : doctorAdvices) {
                    // 医嘱是在规则创建之后开的要才进行提醒
                    if (doctorAdvice.getUpdatedTime().getTime() >= messageRule.getCreatedTime().getTime()) {
                        addMedicineRemindTask(medicine, messageRule, doctorAdvice);
                    }
                }
            }
        }
    }

    /**
     * 添加药物提醒任务
     */
    public synchronized static void addMedicineRemindTask(Medicine medicine, MessageRule messageRule, VtePreventiveDoctorAdvice doctorAdvice) {
        log.info("添加药物提醒任务");
        log.info("药物医嘱 data = {}", JSON.toJSONString(doctorAdvice));
        Long wakeupMinute = MedicineRemindUtil.getMedicineRemindWakeupTime(doctorAdvice, messageRule, SpringContextUtil.getApplicationContext());
        log.info("{} 下一次提醒{}分钟后", Objects.equals(messageRule.getIsLoopWarning().intValue(), MessageRuleLoopWarningEnum.YES.getValue()) ? "循环提醒" : "一次提醒", wakeupMinute);
        executor.schedule(new MedicineRemindThread(medicine, messageRule, doctorAdvice, SpringContextUtil.getApplicationContext()), wakeupMinute, TimeUnit.MINUTES);
    }
}
